﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using RemoteObject;

namespace RemoteClient.UI
{
    public partial class RemoteFileListView : UserControl
    {
        //private List<DownloadJob> preJobs;
        private ClientForm parentCForm;
        private Timer updateTimer;
        private static FileDistributionRequestor fileRequstor;

        public ClientForm ParentCForm
        {
            get
            {
                return parentCForm;
            }

            set
            {
                parentCForm = value;
                InitFileRequestor();
                RefreshFileList();
                InitTimer();            
            }
        }

        public RemoteFileListView()
        {
            InitializeComponent();            
        }

        public RemoteFileListView(ClientForm parentForm)
        {
            this.parentCForm = parentForm;
            InitializeComponent();
            InitFileRequestor();
            RefreshFileList();
            InitTimer();            
        }

        public List<DownloadJob> GetSelectedJobs()
        {
            List<DownloadJob> jobList = new List<DownloadJob>();
            IEnumerator enumerator = fileList.SelectedItems.GetEnumerator();
            while (enumerator.MoveNext())
            {
                RemoteFileItem fileItem = (RemoteFileItem)enumerator.Current;
                jobList.Add(fileItem.DJob);
            }

            return jobList;
        }

        private void InitFileRequestor()
        {
            fileRequstor = FileDistributionRequestor.getInstance();
        }

        private void InitTimer()
        {
            updateTimer = new Timer();
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Interval = 5000;
            updateTimer.Start();
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            Console.WriteLine("-----Update file list--------------");
            RefreshFileList();
        }

        private void RefreshFileList()
        {
            //List<DownloadJob> jobsAdded = new List<DownloadJob>();
            //List<DownloadJob> jobsRemoved = new List<DownloadJob>();
            
            //List<DownloadJob> remoteJobs = fileRequstor.GetFileDistributionInfo();
            //if (preJobs == null)
            //{
            //    preJobs = remoteJobs;
            //    jobsAdded = remoteJobs;
            //}
            //else
            //{
            //    foreach (DownloadJob job in preJobs)
            //    {
            //        if (!remoteJobs.Contains(job))
            //        {
            //            jobsRemoved.Add(job);
            //        }
            //    }

            //    foreach (DownloadJob job in remoteJobs)
            //    {
            //        if (!remoteJobs.Contains(job))
            //        {
            //            jobsAdded.Add(job);
            //        }
            //    }
            //}

            //List<RemoteFileItem> itemsToRemove = new List<RemoteFileItem>();
            //System.Collections.IEnumerator itemEnumerator = fileList.Items.GetEnumerator();
            //while (itemEnumerator.MoveNext())
            //{
            //    RemoteFileItem fileItem = (RemoteFileItem)itemEnumerator.Current;
            //    if(jobsRemoved.Contains(fileItem.DJob))
            //    {
            //        itemsToRemove.Add(fileItem);
            //    }
            //}

            //foreach(RemoteFileItem item in itemsToRemove)
            //{
            //    fileList.Items.Remove(item);
            //}

            //fileList.Items.Clear();
            //foreach(DownloadJob job in remoteJobs)
            //{
            //    fileList.Items.Add(new RemoteFileItem(job));
            //}
            Validate();
        }

        private void downloadMenuItem_Click(object sender, EventArgs e)
        {
            if (parentCForm != null)
            {
                parentCForm.StartNewDownload();
            }
        }
    }
}
